package com.weigee.ktv.v.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONArray;
import com.l.d.app.library.util.ListUtil;
import com.weigee.ktv.R;
import com.weigee.ktv.m.entity.SupermarketChooseGoodsRightEntity;
import com.weigee.ktv.p.TaskDetailContract;
import com.weigee.ktv.p.frag.SupermarketChooseGoodsPresenterImpl;
import com.weigee.ktv.v.adapter.rv.SupermarketChooseGoodsLeftAdapter;
import com.weigee.ktv.v.adapter.rv.SupermarketChooseGoodsRightAdapter;
import com.weigee.ktv.v.view.WrapTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by linde on 2017/04/20.
 * <p/>
 * 挑选超市商品
 */
public class SupermarketChooseGoodsFragment extends BaseFragment2 implements TaskDetailContract.SupermarketChooseGoodsView
{
    private TaskDetailContract.SupermarketChooseGoodsPresenter presenter;

    @BindView(R.id.rv_left)
    RecyclerView rvLeft;
    @BindView(R.id.rv_right)
    RecyclerView rvRight;
    Unbinder unbinder;
    @BindView(R.id.wrap_title_bar)
    WrapTitleBar wrapTitleBar;

    private SupermarketChooseGoodsLeftAdapter leftAdapter;
    private SupermarketChooseGoodsRightAdapter rightAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.supermarket_frag_choosegoods, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);

        wrapTitleBar.setTitle("超市");
        wrapTitleBar.setLeftButton(View.VISIBLE, new SimpleFragBackListener(this));

        initRv();

        request(false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        getPresenter().onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume()
    {
        super.onResume();
        getPresenter().onResume();
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void request(boolean append)
    {
    }

    private void initRv()
    {
        leftAdapter = new SupermarketChooseGoodsLeftAdapter();
        ListUtil.initRecyclerView(rvLeft, null, leftAdapter);

        rightAdapter = new SupermarketChooseGoodsRightAdapter();
        ListUtil.initRecyclerView(rvRight, null, rightAdapter);
    }

    @NonNull
    @Override
    public Context getMyContext()
    {
        final Context result = getActivity();
        return result == null ? getContext() : result;
    }

    @Override
    public TaskDetailContract.SupermarketChooseGoodsPresenter getPresenter()
    {
        if (presenter == null)
        {
            presenter = new SupermarketChooseGoodsPresenterImpl(this);
        }
        return presenter;
    }

    @Override
    public void setGoodsTypeData(@Nullable JSONArray array)
    {
        if (leftAdapter.getItemCount() > 0 && array != null && !array.isEmpty())
        {
            leftAdapter.clear();
        }
        leftAdapter.add(array, String.class);
        leftAdapter.notifyDataSetChanged();
    }

    @Override
    public void setGoodsDetailData(@Nullable JSONArray array, boolean moreData)
    {
        if (array == null || array.isEmpty())
        {
            return;
        }
        if (!moreData && rightAdapter.getItemCount() > 0)
        {
            rightAdapter.clear();
        }
        rightAdapter.add(array, SupermarketChooseGoodsRightEntity.class);
        rightAdapter.notifyDataSetChanged();
    }
}
